package com.earth.c.plugins.mongodb;

import java.util.ArrayList;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;

import com.jfinal.kit.StrKit;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;




@Configuration
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoDBConfiguration {

	private String uri;

	@Bean
	public MongoDatabaseFactory mongoDbFactory() {
		return new SimpleMongoClientDatabaseFactory(uri);
	}

	@Bean
	public MongoDatabaseFactory mongoDbFactory(MongoClientOptionProperties properties) {
		MongoClientOptions.Builder builder = new MongoClientOptions.Builder();

		builder.connectionsPerHost(properties.getMaxConnectionsPerHost());
		builder.minConnectionsPerHost(properties.getMinConnectionsPerHost());
		if (StrKit.notBlank(properties.getReplicaSet())) {
			builder.requiredReplicaSetName(properties.getReplicaSet());
		}
		builder.serverSelectionTimeout(properties.getServerSelectionTimeout());
		builder.maxWaitTime(properties.getMaxWaitTime());
		builder.maxConnectionIdleTime(properties.getMaxConnectionIdleTime());
		builder.maxConnectionLifeTime(properties.getMaxConnectionLifeTime());
		builder.connectTimeout(properties.getConnectTimeout());
		builder.socketTimeout(properties.getSocketTimeout());
		builder.sslEnabled(properties.getSslEnabled());
		builder.sslInvalidHostNameAllowed(properties.getSslInvalidHostNameAllowed());
		builder.heartbeatFrequency(properties.getHeartbeatFrequency());
		builder.minHeartbeatFrequency(properties.getMinHeartbeatFrequency());
		builder.heartbeatConnectTimeout(properties.getHeartbeatConnectTimeout());
		builder.heartbeatSocketTimeout(properties.getHeartbeatSocketTimeout());
		builder.localThreshold(properties.getLocalThreshold());
		MongoClientOptions mongoClientOptions = builder.build();
		// MongoDB地址列表
		List<ServerAddress> serverAddresses = new ArrayList<ServerAddress>();
		for (String address : properties.getAddress()) {
			String[] hostAndPort = address.split(":");
			String host = hostAndPort[0];
			Integer port = Integer.parseInt(hostAndPort[1]);
			ServerAddress serverAddress = new ServerAddress(host, port);
			serverAddresses.add(serverAddress);
		}

//		if (StrKit.notBlank(properties.getUsername())) {
//			MongoCredential mongoCredential=MongoCredential.createScramSha1Credential(properties.getUsername(),
//					StrKit.notBlank(properties.getAuthenticationDatabase()) ? properties.getAuthenticationDatabase()
//							: properties.getDatabase(),
//					properties.getPassword().toCharArray());
//			MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredential, mongoClientOptions);
//		}else {
//			MongoClient mongoClient = new MongoClient(serverAddresses, mongoClientOptions);
//			
//			MongoDatabaseFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(mongoClient, properties.getDatabase());
//			
//		}
		
        //创建客户端和Factory
     //   MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions);
     //   MongoDbFactory mongoDbFactory = new SimpleMongoClientDatabaseFactory(mongoClient, properties.getDatabase());

		
		


		return null;

	}

}
